package rhc.common.util;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Json工具类
 * 
 * @version： v1.0
 * @author huangaigang
 * @date 2015-11-29 23:06:20
 */
public class JsonUtil {
	private final static Logger logger = Logger.getLogger(JsonUtil.class);

	// 将map转换为json
	public static String writeMap2JSON(Map<String, Object> map) {
		String json = "";
		if (map != null && !map.isEmpty()) {
			ObjectMapper objectMapper = new ObjectMapper();
			try {
				json = objectMapper.writeValueAsString(map);
			} catch (Exception e) {
				logger.error("map转json异常：" + e.getMessage());
			}
		}
		return json;
	}

	// 将List转换为json
	public static String writeList2JSON(List<Map<String, Object>> list) {
		String json = "";
		if (list != null && !list.isEmpty()) {
			ObjectMapper objectMapper = new ObjectMapper();
			try {
				json = objectMapper.writeValueAsString(list);
				if (null == json) {
					json = "";
				}
			} catch (Exception e) {
				logger.error("list转json异常：" + e.getMessage());
			}
		}
		return json;
	}

	// 将json转换为map
	@SuppressWarnings("unchecked")
	public static Map<String, Object> readJSON2Map(String json) {
		Map<String, Object> resultMap = Collections.EMPTY_MAP;
		if (null != json && !"".equals(json)) {
			ObjectMapper objectMapper = new ObjectMapper();
			try {
				resultMap = objectMapper.readValue(json, Map.class);
				if (null == resultMap) {
					resultMap = Collections.EMPTY_MAP;
				}
			} catch (Exception e) {
				logger.error("json转map异常：" + e.getMessage());
				resultMap = Collections.EMPTY_MAP;
			}
		}
		return resultMap;

	}

	// 将obj转换为json
	public static String writeEntity2JSON(Object obj) {
		String json = "";
		if (obj != null) {
			ObjectMapper objectMapper = new ObjectMapper();
			try {
				json = objectMapper.writeValueAsString(obj);
				if (null == json) {
					json = "";
				}
			} catch (Exception e) {
				logger.error("对象转json异常：" + e.getMessage());
			}
		}
		return json;
	}

	/**
	 * json 转换List<Map<String, Object>>
	 * 
	 * @param json
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Map<String, Object>> readJSON2MapList(String json) {
		List<Map<String, Object>> list = Collections.EMPTY_LIST;
		if (null != json && !"".equals(json)) {
			ObjectMapper objectMapper = new ObjectMapper();
			JavaType javaType = getCollectionType(List.class, Map.class);
			try {
				list = (List<Map<String, Object>>) objectMapper.readValue(json, javaType);
			} catch (Exception e) {
				logger.error("json转List异常：" + e.getMessage());
			}
		}
		return list;
	}

	/**
	 * 获取到将json转换为的集合的泛型的类型
	 * 
	 * @param collectionClass
	 *            集合的类型
	 * @param elementClasses
	 *            泛型的类型
	 * @return collectionClass<elementClasses>
	 */
	private static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
		ObjectMapper objectMapper = new ObjectMapper();
		return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
	}

	@SuppressWarnings("unchecked")
	public static List<String> readJSON2List(String json) {
		List<String> list = Collections.EMPTY_LIST;
		if (null != json && !"".equals(json)) {
			ObjectMapper objectMapper = new ObjectMapper();
			JavaType javaType = getCollectionType(List.class, String.class);
			try {
				list = (List<String>) objectMapper.readValue(json, javaType);
			} catch (Exception e) {
				logger.error("json转List异常：" + e.getMessage());
			}
		}
		return list;
	}

	/**
	 * 转JSONArray
	 * 
	 * @param obj
	 * @return
	 */
	public static JSONArray Json2JSONArray(Object obj) {
		JSONArray array = null;
		try {
			array = new JSONArray(String.valueOf(obj));
		} catch (Exception e) {
			logger.error("转JSONArray异常：" + e.getMessage());
			new JSONArray();
		}
		return array;
	}

	/**
	 * 转JSONObject
	 * 
	 * @param o
	 * @return
	 */
	public static JSONObject Json2Obj(Object o) {
		JSONObject obj = null;
		try {
			obj = new JSONObject(String.valueOf(o));
		} catch (Exception e) {
			logger.error("String转JSONObject异常：" + e.getMessage());
			obj = new JSONObject();
		}
		return obj;
	}

	/**
	 * 判断是否是json结构
	 */
	public static boolean isJson(String value) {
		try {
			new JSONObject(value);
		} catch (JSONException e) {
			return false;
		}
		return true;
	}
	
	/**从map中根据路径获取值
	 * @param map
	 * @param pathList
	 * @return
	 */
	public static Object fromMapPathGetValue(Map<String,Object> map,List<String> pathList){
		if(null == pathList || pathList.isEmpty() || null == map){
			return null;
		}
		if(!(pathList instanceof LinkedList)){
			pathList=new LinkedList<String>(pathList);
		}
		String path=pathList.remove(0);
		Object value=map.get(path);
		
		if(pathList.isEmpty()){
			return value;
		}else if(value instanceof Map){
			return fromMapPathGetValue((Map<String,Object>)value,pathList);
		}else{
			logger.error("获取 map path 路径错误，获取不到值");
			return null;
		}
	}
}
